﻿myApp.controller('AuctionController', function ($scope, $http, DUMMY_DATA,$location, $rootScope) {
    $scope.$emit('newPageLoaded', {
        'title': 'Auction Property | ShiJie Property',
        'description': 'Auction Property. We Work Very Closely With Developers. Over 40 Years Experience. Discover The Best New Properties For Auction Today!',
        'keywords': 'auction property, auction properties, property auction'
    });

    //$scope.$emit('newPageLoaded', { 'title': 'Properties Auction in Penang', 'description': 'Buy below market price! House/Factory/Land/Condo/Shop' });
    $scope.isAndroid = $rootScope.isAndroid;

    pageInit();

    $scope.sortOption = "Sorted by High to Low";

    $scope.isLogin = $rootScope.isAuth;


    var BASE_URL;
    if ($location.host() == "localhost") {
        BASE_URL = $location.protocol() + "://" + "localhost:" + $location.port();
    } else {
        BASE_URL = $location.protocol() + "://" + $location.host();
    }


    $scope.getShareURL = function (p) {
        //  return (BASE_URL + "/property/" + p.propertyid + "/S" + p.Zoning[0]);
        return (BASE_URL + "/property/auction/details/ " + p.LelongId);
    }

    $scope.getShareTitle = function (p) {
        return p.Description;
    }

    $scope.getShareDescription = function (p) {
        return p.RoadName;
    }

    $scope.getShareImageURL = function (p) {
        return (p.PropertyURL || "https://c1.staticflickr.com/3/2280/32796837675_cd6b733c19_m.jpg");
    }


    $scope.paginate = function (value) {
        var begin, end, index;
        begin = ($scope.currentPage - 1) * $scope.numPerPage;
        end = begin + $scope.numPerPage;
        index = $scope.searchData.indexOf(value);
        return (begin <= index && index < end);
    };
    $scope.updateFilterOption = function (option, sortedChanged) {
        $scope.SortOptionChanged = sortedChanged;
        //$scope.$apply();
        console.log("HEY!" + option);
        switch (option) {
            case 0:
                $scope.sortOption = "Sorted by High to Low";
                $scope.sortKey = '-Price';
                break;
            case 1:
                $scope.sortOption = "Sorted by Low to High";
                $scope.sortKey = 'Price';
                break;
            case 2:
                $scope.sortOption = "Sorted by Area (A to Z)";
                $scope.sortKey = 'Description';
                break;
            case 3:
                $scope.sortOption = "Sorted by Area (Z to A)";
                $scope.sortKey = '-Description';
                break;
        }
    }

    $scope.isEmpty = function (obj) {
        for (var i in obj) if (obj.hasOwnProperty(i)) return false;
        return true;
    };


    $scope.is10Selected = true;
    $scope.is25Selected = false;
    $scope.is50Selected = false;
    $scope.is100Selected = false;
    $scope.changePerPage = function (npp) {
        $scope.numPerPage = npp;
        switch (npp) {
            case 10:
                $scope.is10Selected = true;
                $scope.is25Selected = false;
                $scope.is50Selected = false;
                $scope.is100Selected = false;
                break;
            case 25:
                $scope.is10Selected = false;
                $scope.is25Selected = true;
                $scope.is50Selected = false;
                $scope.is100Selected = false;
                break;
            case 50:
                $scope.is10Selected = false;
                $scope.is25Selected = false;
                $scope.is50Selected = true;
                $scope.is100Selected = false;
                break;
            case 100:
                $scope.is10Selected = false;
                $scope.is25Selected = false;
                $scope.is50Selected = false;
                $scope.is100Selected = true;
                break;
        }
        $scope.totalItems = $scope.searchData.length;
        $scope.currentPage = 1;
        $scope.numPerPage = npp;
    }



    function pageInit() {
        if (DUMMY_DATA == true) {
            var dummyData = [{ "LelongId": "db1e941a-3a28-4948-b68d-bd0bb1b5bf5c", "LelongTipCode": "", "Address": "3A-07,Block A, Sutera Apartment, Jalan Sutera 3, Taman Sutera, SeberANG Jaya, 13700 Perai, Seberang Perai Tengah, Penang.", "Price": 75000.00, "Type": "Apartment/Condo", "LandArea": 650.0, "AuctionDate": "2016-11-28T00:00:00", "Description": "Sutera Apartment, Low Cost", "AgentName": "MIKI LIM CHUN SIEW", "StaffPicURL": "https://farm8.staticflickr.com/7360/27380694890_80ba24fab0_m.jpg", "AgentMobile": "0134817777", "CreateDate": "2016-11-14T15:31:25.987", "UpdateDate": "2016-11-14T15:33:52.947", "PropertyURL": null }, { "LelongId": "72e3f555-d96f-411f-a73b-cfc50a03ec4e", "LelongTipCode": "", "Address": "112B-G-5,Jalan Permatang Damar Laut, 11960 Bayan Lepas, Penang.", "Price": 100000.00, "Type": "Flat", "LandArea": 549.0, "AuctionDate": "2016-12-28T00:00:00", "Description": "Flat", "AgentName": "MIKI LIM CHUN SIEW", "StaffPicURL": "https://farm8.staticflickr.com/7360/27380694890_80ba24fab0_m.jpg", "AgentMobile": "0134817777", "CreateDate": "2016-11-14T17:02:23.21", "UpdateDate": null, "PropertyURL": null }, { "LelongId": "02f513e6-9795-43b3-bad2-d44661bfce45", "LelongTipCode": "", "Address": "7-5-8,Tingkat Paya Terubong 2, Mukim 13, 11060, Penang.", "Price": 90000.00, "Type": "Flat", "LandArea": 538.0, "AuctionDate": "2016-12-20T00:00:00", "Description": "Flat", "AgentName": "MIKI LIM CHUN SIEW", "StaffPicURL": "https://farm8.staticflickr.com/7360/27380694890_80ba24fab0_m.jpg", "AgentMobile": "0134817777", "CreateDate": "2016-11-14T16:31:11.72", "UpdateDate": null, "PropertyURL": null }, { "LelongId": "9a8a7856-b6e5-4ab8-a9dc-d5ddbcf1cfde", "LelongTipCode": "", "Address": "9-14-6,Lengkok Angsana, Sri Impian, Bandar Baru Ait Itam, 11500, Penang.", "Price": 260000.00, "Type": "Flat", "LandArea": 700.0, "AuctionDate": "2016-12-21T00:00:00", "Description": "Flat", "AgentName": "MIKI LIM CHUN SIEW", "StaffPicURL": "https://farm8.staticflickr.com/7360/27380694890_80ba24fab0_m.jpg", "AgentMobile": "0134817777", "CreateDate": "2016-11-14T16:55:01.81", "UpdateDate": null, "PropertyURL": null }, { "LelongId": "6631df0f-c24b-45e1-ba96-f7c880751b1e", "LelongTipCode": "", "Address": "113-5-10,Persiaran Bayan Indah, Mukim 12, 11950 Bayan Lepas, Penang.", "Price": 530000.00, "Type": "Apartment/Condo", "LandArea": 1130.0, "AuctionDate": "2016-12-03T00:00:00", "Description": "Condominium", "AgentName": "MIKI LIM CHUN SIEW", "StaffPicURL": "https://farm8.staticflickr.com/7360/27380694890_80ba24fab0_m.jpg", "AgentMobile": "0134817777", "CreateDate": "2016-11-14T16:12:02.44", "UpdateDate": null, "PropertyURL": null }, { "LelongId": "49572eb3-32e5-4a95-9a95-08f82dfb09e1", "LelongTipCode": "", "Address": "33-3-19,Jalan Dr.Lim Chwee Leong, Prangin Mall, 10100, Penang.", "Price": 530000.00, "Type": "Shop/Office", "LandArea": 724.0, "AuctionDate": "2016-11-23T00:00:00", "Description": "Shop Lot", "AgentName": "MIKI LIM CHUN SIEW", "StaffPicURL": "https://farm8.staticflickr.com/7360/27380694890_80ba24fab0_m.jpg", "AgentMobile": "0134817777", "CreateDate": "2016-11-14T16:21:28.237", "UpdateDate": null, "PropertyURL": null }, { "LelongId": "17ca25ff-6437-4b72-a359-1293b6cadf47", "LelongTipCode": "", "Address": "8C-15-06,Asia Height, Lorong Semarak 3, 11500, Penang", "Price": 400000.00, "Type": "Apartment/Condo", "LandArea": 786.0, "AuctionDate": "2016-11-15T00:00:00", "Description": "Asia Heights Apartment", "AgentName": "MIKI LIM CHUN SIEW", "StaffPicURL": "https://farm8.staticflickr.com/7360/27380694890_80ba24fab0_m.jpg", "AgentMobile": "0134817777", "CreateDate": "2016-10-31T11:32:33.137", "UpdateDate": null, "PropertyURL": null }, { "LelongId": "3fbf32d3-c8c1-4cb3-a218-17f6f125596d", "LelongTipCode": "", "Address": "5,Lorong Seri Bistari 12, Taman Bistari 12, Taman Seri Bistari, 14300 Nibong Tebal, Penang.", "Price": 109350.00, "Type": "House", "LandArea": 1302.0, "AuctionDate": "2016-11-30T00:00:00", "Description": "Terrace House", "AgentName": "MIKI LIM CHUN SIEW", "StaffPicURL": "https://farm8.staticflickr.com/7360/27380694890_80ba24fab0_m.jpg", "AgentMobile": "0134817777", "CreateDate": "2016-11-15T14:12:22.98", "UpdateDate": null, "PropertyURL": null }, { "LelongId": "73e98b8a-4711-4f1e-8e57-2d93acf8816a", "LelongTipCode": "", "Address": "8,Lorong Usahajaya 3, Kawasan Perusahaan Ringan Usahajaya, 34400 Simpang Ampat, Semanggol, Perak.", "Price": 400000.00, "Type": "Factory", "LandArea": 1873.0, "AuctionDate": "2016-12-20T00:00:00", "Description": "1.5 Storey Factory, Intermediate", "AgentName": "MIKI LIM CHUN SIEW", "StaffPicURL": "https://farm8.staticflickr.com/7360/27380694890_80ba24fab0_m.jpg", "AgentMobile": "0134817777", "CreateDate": "2016-11-14T16:34:58.467", "UpdateDate": null, "PropertyURL": null }, { "LelongId": "7442a050-82dd-4ffd-84ed-387e99479754", "LelongTipCode": "", "Address": "3,Lengkok Relau 1, Taman Seri RElau, 11900, Penang.", "Price": 1500000.00, "Type": "House", "LandArea": 2379.0, "AuctionDate": "2016-12-28T00:00:00", "Description": "3 Storey Semi Detached House", "AgentName": "MIKI LIM CHUN SIEW", "StaffPicURL": "https://farm8.staticflickr.com/7360/27380694890_80ba24fab0_m.jpg", "AgentMobile": "0134817777", "CreateDate": "2016-11-14T16:58:49.783", "UpdateDate": null, "PropertyURL": null }, { "LelongId": "cdca9fcf-8b70-4a67-ae8a-4010a70f1718", "LelongTipCode": "", "Address": "7A-6-15,Pangsapuri Palm Court, Seru Tanjung Pinang, 10470 Tanjong Tokong, Penang.", "Price": 150000.00, "Type": "Apartment/Condo", "LandArea": 689.0, "AuctionDate": "2016-12-03T00:00:00", "Description": "Palm Court Apartment", "AgentName": "MIKI LIM CHUN SIEW", "StaffPicURL": "https://farm8.staticflickr.com/7360/27380694890_80ba24fab0_m.jpg", "AgentMobile": "0134817777", "CreateDate": "2016-11-14T16:09:25.843", "UpdateDate": null, "PropertyURL": null }, { "LelongId": "39f9e3ec-e11d-4caa-a5ac-4f27d0be54bf", "LelongTipCode": "", "Address": "2-1-14,1st Floor,Block 2, Lintang Sungai Batu, Taman Teluk Kumbar, 11920, Penang.", "Price": 80000.00, "Type": "Flat", "LandArea": 463.0, "AuctionDate": "2016-12-21T00:00:00", "Description": "Flat. Low Cost", "AgentName": "MIKI LIM CHUN SIEW", "StaffPicURL": "https://farm8.staticflickr.com/7360/27380694890_80ba24fab0_m.jpg", "AgentMobile": "0134817777", "CreateDate": "2016-10-31T11:52:35.37", "UpdateDate": null, "PropertyURL": null }, { "LelongId": "5e3e71fb-46c8-45f3-9f9e-5730980b7330", "LelongTipCode": "", "Address": "716-2-11,Flat MPPP, Jalan Sungai Dua, Sungai Nibong, 11900, Penang.", "Price": 75600.00, "Type": "Flat", "LandArea": 433.0, "AuctionDate": "2016-11-26T00:00:00", "Description": "MPPP Flat, Low Cost", "AgentName": "MIKI LIM CHUN SIEW", "StaffPicURL": "https://farm8.staticflickr.com/7360/27380694890_80ba24fab0_m.jpg", "AgentMobile": "0134817777", "CreateDate": "2016-11-14T16:16:09.57", "UpdateDate": null, "PropertyURL": null }, { "LelongId": "cb2b2e7d-195f-4acd-9da4-6511602e6492", "LelongTipCode": "", "Address": "16,Jalan Moonlight Bay, Villa Moonlight Bay, 11100 Batu Ferrighi, Penang.", "Price": 2500000.00, "Type": "House", "LandArea": 4004.0, "AuctionDate": "2016-11-22T00:00:00", "Description": "4 Storey Detached Villa", "AgentName": "MIKI LIM CHUN SIEW", "StaffPicURL": "https://farm8.staticflickr.com/7360/27380694890_80ba24fab0_m.jpg", "AgentMobile": "0134817777", "CreateDate": "2016-11-14T16:24:08.62", "UpdateDate": null, "PropertyURL": null }, { "LelongId": "d5ac6a29-cb68-4c88-8276-751462584fde", "LelongTipCode": "", "Address": "29,Lorong 18/SS3, Bandar Tasek Mutiara, Simpang Ampat, 14120, Penang.", "Price": 220000.00, "Type": "House", "LandArea": 2098.0, "AuctionDate": "2016-12-03T00:00:00", "Description": "1 Storey Terrace House, Intermediate", "AgentName": "MIKI LIM CHUN SIEW", "StaffPicURL": "https://farm8.staticflickr.com/7360/27380694890_80ba24fab0_m.jpg", "AgentMobile": "0134817777", "CreateDate": "2016-11-14T16:06:21.56", "UpdateDate": null, "PropertyURL": null }, { "LelongId": "9a6a2ea0-840f-4214-bf0c-7f3bbcfd4f27", "LelongTipCode": "", "Address": "2,Lorong Penaga Permai 20, Taman Penaga Permai, 13100 Penaga, Penang.", "Price": 95000.00, "Type": "House", "LandArea": 629.0, "AuctionDate": "2016-11-28T00:00:00", "Description": "Town House", "AgentName": "MIKI LIM CHUN SIEW", "StaffPicURL": "https://farm8.staticflickr.com/7360/27380694890_80ba24fab0_m.jpg", "AgentMobile": "0134817777", "CreateDate": "2016-11-14T15:39:06.64", "UpdateDate": null, "PropertyURL": null }, { "LelongId": "084386b1-0c7c-46dc-b2cd-85f00732453b", "LelongTipCode": "", "Address": "9A-17-8,Taman Kheng Tian, Jalan Van Praagh, Jelutong, 11600, Penang.", "Price": 300000.00, "Type": "Apartment/Condo", "LandArea": 700.0, "AuctionDate": "2016-12-03T00:00:00", "Description": "Apartment", "AgentName": "MIKI LIM CHUN SIEW", "StaffPicURL": "https://farm8.staticflickr.com/7360/27380694890_80ba24fab0_m.jpg", "AgentMobile": "0134817777", "CreateDate": "2016-11-14T16:02:42.253", "UpdateDate": null, "PropertyURL": null }, { "LelongId": "ff7c7f73-677a-45cd-ae0b-9e7614d41953", "LelongTipCode": "", "Address": "2-6-8,Gat Lebuh Macallum,Harbour Trade Centre, 10300 Georgetown, Penang.", "Price": 315900.00, "Type": "Shop/Office", "LandArea": 2336.0, "AuctionDate": "2016-11-22T00:00:00", "Description": "Shop Lot", "AgentName": "MIKI LIM CHUN SIEW", "StaffPicURL": "https://farm8.staticflickr.com/7360/27380694890_80ba24fab0_m.jpg", "AgentMobile": "0134817777", "CreateDate": "2016-11-14T16:18:41.5", "UpdateDate": null, "PropertyURL": null }, { "LelongId": "a69f2c93-aff3-4b26-b585-a692d442ddf6", "LelongTipCode": "", "Address": "3-15,Pangsapuri Block A, Jalan Mewah 2, 12100 Butterworth, Penang.", "Price": 110000.00, "Type": "Apartment/Condo", "LandArea": 753.0, "AuctionDate": "2016-12-06T00:00:00", "Description": "Apartment", "AgentName": "MIKI LIM CHUN SIEW", "StaffPicURL": "https://farm8.staticflickr.com/7360/27380694890_80ba24fab0_m.jpg", "AgentMobile": "0134817777", "CreateDate": "2016-11-14T17:08:18.75", "UpdateDate": null, "PropertyURL": null }, { "LelongId": "37546913-c03f-4804-a065-b1a65065be73", "LelongTipCode": "", "Address": "215,Pintasan Mayang 1, 11950 Bayan Baru, Penang.", "Price": 130000.00, "Type": "House", "LandArea": 538.0, "AuctionDate": "2016-12-13T00:00:00", "Description": "2 Storey Cluster House, Intermediate", "AgentName": "MIKI LIM CHUN SIEW", "StaffPicURL": "https://farm8.staticflickr.com/7360/27380694890_80ba24fab0_m.jpg", "AgentMobile": "0134817777", "CreateDate": "2016-11-14T16:28:11.853", "UpdateDate": null, "PropertyURL": null }, { "LelongId": "6247fe83-9a20-4eb0-88be-ba8a6c5fdd34", "LelongTipCode": "", "Address": "1B-15-5,Desa Baiduri, Jalan Ru1, Bandar Baru Ayer Itam, 11500 Air Hitam, Penang.", "Price": 159000.00, "Type": "Apartment/Condo", "LandArea": 624.0, "AuctionDate": "2016-12-06T00:00:00", "Description": "Desa Baiduri Apartment", "AgentName": "MIKI LIM CHUN SIEW", "StaffPicURL": "https://farm8.staticflickr.com/7360/27380694890_80ba24fab0_m.jpg", "AgentMobile": "0134817777", "CreateDate": "2016-11-14T17:14:32.043", "UpdateDate": null, "PropertyURL": null }, { "LelongId": "44b1ff94-97ed-4c97-8dbf-ba91d7445295", "LelongTipCode": "", "Address": "11-4-5,Desa Golf Condominium, Persiaran Bukit Jambul 2, Bukit Jambul, 11950 Bayan Lepas, Penang.", "Price": 340200.00, "Type": "Apartment/Condo", "LandArea": 850.0, "AuctionDate": "2016-11-30T00:00:00", "Description": "Desa Golf Condominium", "AgentName": "MIKI LIM CHUN SIEW", "StaffPicURL": "https://farm8.staticflickr.com/7360/27380694890_80ba24fab0_m.jpg", "AgentMobile": "0134817777", "CreateDate": "2016-11-09T09:48:30.373", "UpdateDate": null, "PropertyURL": null }];

            $scope.searchData = dummyData;
            console.log($scope.searchData.length);
            $scope.totalItems = $scope.searchData.length;
            $scope.currentPage = 1;
            $scope.numPerPage = 10;

        } else {
            $http.get('api/Auction/GetAuction').success(function (data) {
                console.log("New Auction: " + JSON.stringify(data));
                $scope.searchData = data;
                $scope.totalItems = $scope.searchData.length;
                $scope.currentPage = 1;
                $scope.numPerPage = 10;
            });
        }

       
    }



});